package com.enation.app.shop.front.policyInsurance.service.impl;


import com.enation.app.shop.front.api.inpolicy.entity.InPolicy;
import com.enation.app.shop.front.policyInsurance.entity.ClaimApplication;
import com.enation.app.shop.front.policyInsurance.service.ClaimApplicationMapper;
import com.enation.framework.database.IDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("claimApplicationService")
public class ClaimApplicationService implements ClaimApplicationMapper{

    @Autowired
    private IDaoSupport daoSupport;
    @Override
    public int deleteByPrimaryKey(Long id) {

        return 0;
    }

    @Override
    public int insert(ClaimApplication record) {
        this.daoSupport.insert("rbt_claim_application",record);
        int i= this.daoSupport.getLastId("rbt_claim_application");
        return i;
    }

    @Override
    public int insertSelective(ClaimApplication record) {
        return 0;
    }

    @Override
    public ClaimApplication selectByPrimaryKey(Long id) {
        String sql="select * from rbt_claim_application where id=?";
        ClaimApplication claimApplication=this.daoSupport.queryForObject(sql,ClaimApplication.class,id);
        return claimApplication;
    }

    @Override
    public int updateByPrimaryKeySelective(ClaimApplication record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(ClaimApplication record) {
        this.daoSupport.update("rbt_claim_application",record,"id="+record.getId());
        return 1;
    }

    @Override
    public int getClaimCount(Long breed_factory_id) {
        //String sql="select count(*) from rbt_claim_application where id=(select max(id) from rbt_claim_application where breed_factory_id=?)";
        String sql="select count(*) from rbt_claim_application where breed_factory_id=?";
        return this.daoSupport.queryForInt(sql,breed_factory_id);
    }

    @Override
    public List<ClaimApplication> listByUserid(Long breed_factory_id) {
        String sql="select * from rbt_claim_application where breed_factory_id=?";
        return this.daoSupport.queryForList(sql,ClaimApplication.class,breed_factory_id);
    }

    @Override
    public List<InPolicy> getWarrantyByuserid(Integer userid) {
        String sql="select * from rbt_inpolicy where user_id=?";
        return this.daoSupport.queryForList(sql,InPolicy.class,userid);
    }

    @Override
    public List<ClaimApplication> listByUseridAndStatusFac(Integer userid, Integer status,String item) {
        if (status==null){
            if (item==null){
                String sql="select * from rbt_claim_application where userid=? and year(application_date)='"+2018+"' ";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid);
            }else {
                String sql="select * from rbt_claim_application where userid=?   and item like '%"+item+"%' and year(application_date)='"+2018+"' ";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid);
            }
        }else {
            if (item==null){
                String sql="select * from rbt_claim_application where userid=?  and check_status=? and year(application_date)='"+2018+"'";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid,status);
            }else {
                String sql="select * from rbt_claim_application where userid=? and check_status=?  and item like '%"+item+"%' and year(application_date)='"+2018+"'";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid,status);
            }

        }


    }

    @Override
    public List<ClaimApplication> listByUseridAndStatusIns(Integer userid, Integer status, String item) {
        if (status==null){
            if (item==null){
                String sql="select * from rbt_claim_application where receiver_id=? and year(application_date)='"+2018+"' ";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid);
            }else {
                String sql="select * from rbt_claim_application where receiver_id=?   and item like '%"+item+"%' and year(application_date)='"+2018+"' ";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid);
            }
        }else {
            if (item==null){
                String sql="select * from rbt_claim_application where receiver_id=?  and check_status=? and year(application_date)='"+2018+"'";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid,status);
            }else {
                String sql="select * from rbt_claim_application where receiver_id=? and check_status=?  and item like '%"+item+"%' and year(application_date)='"+2018+"'";
                return this.daoSupport.queryForList(sql,ClaimApplication.class,userid,status);
            }

        }

    }

    @Override
    public List<ClaimApplication> getByWarrantyid(Integer warrantyid) {
        String sql="select * from  rbt_claim_application where warranty_id=?";
        return this.daoSupport.queryForList(sql,ClaimApplication.class,warrantyid);
    }

    @Override
    public List<ClaimApplication> getAll() {
        String sql="select * from rbt_claim_application";
        return this.daoSupport.queryForList(sql,ClaimApplication.class);
    }

    @Override
    public List<ClaimApplication> getByPage(int i, Integer limit, String seacher) {
        if (seacher==null){
            String sql="select * from  rbt_claim_application limit ?,?";
            return this.daoSupport.queryForList(sql,ClaimApplication.class,i,limit);
        }else {
            String sql="select * from  rbt_claim_application  where breed_factory_name like '%"+seacher+"%' limit ?,?";
            return this.daoSupport.queryForList(sql,ClaimApplication.class,i,limit);
        }
    }

    @Override
    public List<ClaimApplication> getBySercher(String seacher) {
        String sql="select * from  rbt_claim_application where breed_factory_name like '%"+seacher+"%'";
        return this.daoSupport.queryForList(sql,ClaimApplication.class);
    }
}
